package com.restaurant.application.indexes.genshanguserList.mysql;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.nebutown.base.Unit;
import com.nebutown.description.data.SortSpec;
import com.nebutown.generation.basictype.UID;
import com.nebutown.supporthome.AbstractIndex;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

public class GenShangUserListImpl extends AbstractIndex<GenShangUserListCriteria, GenShangUserListBean> {

    @Override
    public String getTableName() {
        return "" + getConnectionName() + ".GENSHANGUSERLIST";
    }

    @Override
    public GenShangUserListBean newBean() {
        return new GenShangUserListBean();
    }

    @Override
    public GenShangUserListCriteria newCriteria() {
        return new GenShangUserListCriteria();
    }

    @Override
    public List<String> getFieldNames() {
        return Lists.newArrayList(
                "userUid",
                "id",
                "number",
                "createTime",
                "modifyTime",
                "avatarUrl",
                "nickName",
                "realName",
                "gender",
                "password",
                "phone",
                "state");
    }

    @Override
    public List<String> getDateFields() {
        return Lists.newArrayList(
                "createTime",
                "modifyTime");
    }

    @Override
    public List<String> getDoubleFields() {
        return Lists.newArrayList(
                "");
    }

    @Override
    public Map<String, String> getFieldLabels() {
        return ImmutableMap.<String, String>builder()
                .put("id", "用户id")
                .put("number", "用户id")
                .put("createTime", "创建时间")
                .put("modifyTime", "修改时间")
                .put("avatarUrl", "头像地址")
                .put("nickName", "登陆名称")
                .put("realName", "登陆名称")
                .put("gender", "性别")
                .put("password", "密码")
                .put("phone", "手机")
                .put("state", "状态")
                .build();
    }

    @Override
    public List<String> getContributors() {
        return Lists.newArrayList(
                "com.restaurant.application.genshanguser.GenShangUser");
    }

    @Override
    public GenShangUserListBean fakeBean() {
        GenShangUserListBean bean = new GenShangUserListBean();

        bean.setUserUid(UID.recover("A -7681036492558982305 looooong string"));
        bean.setId("A 919292151635295017 looooong string");
        bean.setNumber("A 7905801278531525003 looooong string");
        bean.setCreateTime(-995047726L);
        bean.setModifyTime(1887381237L);
        bean.setAvatarUrl("A 4020945128410800565 looooong string");
        bean.setNickName("A 6198428936997403023 looooong string");
        bean.setRealName("A -1230015648175682375 looooong string");
        bean.setGender("A 3285228730764688349 looooong string");
        bean.setPassword("A -6978444774544262061 looooong string");
        bean.setPhone("A 5492601951436513583 looooong string");
        bean.setState("A -8735974801791013949 looooong string");
        return bean;
    }

    @Override
    public String getCreateTableStatement() {
        StringBuilder buf = new StringBuilder();

        buf.append("CREATE TABLE IF NOT EXISTS " + getConnectionName() + ".GENSHANGUSERLIST (\n");
        buf.append("    RECORD_ID BIGINT AUTO_INCREMENT,\n");
        buf.append("    USERUID CHAR(128) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,\n");
        buf.append("    ID VARCHAR(255),\n");
        buf.append("    NUMBER VARCHAR(255),\n");
        buf.append("    CREATETIME BIGINT,\n");
        buf.append("    MODIFYTIME BIGINT,\n");
        buf.append("    AVATARURL VARCHAR(255),\n");
        buf.append("    NICKNAME VARCHAR(255),\n");
        buf.append("    REALNAME VARCHAR(255),\n");
        buf.append("    GENDER VARCHAR(255),\n");
        buf.append("    PASSWORD VARCHAR(255),\n");
        buf.append("    PHONE VARCHAR(255),\n");
        buf.append("    STATE VARCHAR(255),\n");
        buf.append("PRIMARY KEY (RECORD_ID)) CHARACTER SET  = utf8mb4 COLLATE = utf8mb4_bin");
        return buf.toString();
    }

    @Override
    public String getInsertDataStatement(GenShangUserListBean data) {
        StringBuilder buf = new StringBuilder();

        buf.append("INSERT INTO " + getConnectionName() + ".GENSHANGUSERLIST (\n    ");
        buf.append("    USERUID, ID, NUMBER, CREATETIME, MODIFYTIME, AVATARURL, NICKNAME, REALNAME, GENDER, PASSWORD, PHONE, STATE");
        buf.append(") VALUES (");
        buf.append("    '" + cut(data.getUserUid(), 128) + "', '" + cut(data.getId(), 255) + "', '" + cut(data.getNumber(), 255) + "', " + data.getCreateTime() + ", " + data.getModifyTime() + ", '" + cut(data.getAvatarUrl(), 255) + "', '" + cut(data.getNickName(), 255) + "', '" + cut(data.getRealName(), 255) + "', '" + cut(data.getGender(), 255) + "', '" + cut(data.getPassword(), 255) + "', '" + cut(data.getPhone(), 255) + "', '" + cut(data.getState(), 255) + "'");
        buf.append(")");
        return buf.toString();
    }

    @Override
    public String getDeleteRowsStatement(Long rowId) {
        return "DELETE FROM " + getConnectionName() + ".GENSHANGUSERLIST WHERE  RECORD_ID = " + rowId;
    }

    @Override
    public String getLocalUpdateStatement(GenShangUserListBean data) {
        StringBuilder buf = new StringBuilder();
        StringBuilder sep = new StringBuilder();

        boolean hasData = false;

        buf.append("UPDATE " + getConnectionName() + ".GENSHANGUSERLIST \n");
        buf.append("   SET ");
        if (data.getUserUid() != null) {
            buf.append(sep.toString() + "\n      USERUID = '" + cut(data.getUserUid(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getId() != null) {
            buf.append(sep.toString() + "\n      ID = '" + cut(data.getId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getNumber() != null) {
            buf.append(sep.toString() + "\n      NUMBER = '" + cut(data.getNumber(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getCreateTime() != null) {
            buf.append(sep.toString() + "\n      CREATETIME = " + data.getCreateTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getModifyTime() != null) {
            buf.append(sep.toString() + "\n      MODIFYTIME = " + data.getModifyTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getAvatarUrl() != null) {
            buf.append(sep.toString() + "\n      AVATARURL = '" + cut(data.getAvatarUrl(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getNickName() != null) {
            buf.append(sep.toString() + "\n      NICKNAME = '" + cut(data.getNickName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getRealName() != null) {
            buf.append(sep.toString() + "\n      REALNAME = '" + cut(data.getRealName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getGender() != null) {
            buf.append(sep.toString() + "\n      GENDER = '" + cut(data.getGender(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getPassword() != null) {
            buf.append(sep.toString() + "\n      PASSWORD = '" + cut(data.getPassword(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getPhone() != null) {
            buf.append(sep.toString() + "\n      PHONE = '" + cut(data.getPhone(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getState() != null) {
            buf.append(sep.toString() + "\n      STATE = '" + cut(data.getState(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (!hasData) return "";

        hasData = false;
        buf.append(" WHERE TRUE\n");
        if (data.getUserUid() != null) {
            buf.append("    AND USERUID = '" + data.getUserUid() + "'\n");
            hasData = true;
        }
        if (!hasData) return "";

        return buf.toString();
    }

    @Override
    public String getUpdateByIdStatement(GenShangUserListBean data) {
        StringBuilder buf = new StringBuilder();
        StringBuilder sep = new StringBuilder();

        boolean hasData = false;

        buf.append("UPDATE " + getConnectionName() + ".GENSHANGUSERLIST \n");
        buf.append("   SET ");
        if (data.getUserUid() != null) {
            buf.append(sep.toString() + "\n      USERUID = '" + cut(data.getUserUid(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getId() != null) {
            buf.append(sep.toString() + "\n      ID = '" + cut(data.getId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getNumber() != null) {
            buf.append(sep.toString() + "\n      NUMBER = '" + cut(data.getNumber(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getCreateTime() != null) {
            buf.append(sep.toString() + "\n      CREATETIME = " + data.getCreateTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getModifyTime() != null) {
            buf.append(sep.toString() + "\n      MODIFYTIME = " + data.getModifyTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getAvatarUrl() != null) {
            buf.append(sep.toString() + "\n      AVATARURL = '" + cut(data.getAvatarUrl(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getNickName() != null) {
            buf.append(sep.toString() + "\n      NICKNAME = '" + cut(data.getNickName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getRealName() != null) {
            buf.append(sep.toString() + "\n      REALNAME = '" + cut(data.getRealName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getGender() != null) {
            buf.append(sep.toString() + "\n      GENDER = '" + cut(data.getGender(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getPassword() != null) {
            buf.append(sep.toString() + "\n      PASSWORD = '" + cut(data.getPassword(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getPhone() != null) {
            buf.append(sep.toString() + "\n      PHONE = '" + cut(data.getPhone(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getState() != null) {
            buf.append(sep.toString() + "\n      STATE = '" + cut(data.getState(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (!hasData) return "";

        hasData = false;
        buf.append(" WHERE RECORD_ID = " + data.getRecordId());
        return buf.toString();
    }

    @Override
    public GenShangUserListBean extractLine(ResultSet resultSet) throws Exception {
        GenShangUserListBean result = new GenShangUserListBean();

        result.setRecordId(resultSet.getLong("RECORD_ID"));
        result.setUserUid(UID.recover(resultSet.getString("userUid")));
        result.setId(resultSet.getString("id"));
        result.setNumber(resultSet.getString("number"));
        result.setCreateTime(resultSet.getLong("createTime"));
        result.setModifyTime(resultSet.getLong("modifyTime"));
        result.setAvatarUrl(resultSet.getString("avatarUrl"));
        result.setNickName(resultSet.getString("nickName"));
        result.setRealName(resultSet.getString("realName"));
        result.setGender(resultSet.getString("gender"));
        result.setPassword(resultSet.getString("password"));
        result.setPhone(resultSet.getString("phone"));
        result.setState(resultSet.getString("state"));
        return result;
    }

    @Override
    public String getWherePart(GenShangUserListCriteria criteria, SortSpec sort) {
        StringBuilder buf = new StringBuilder(" TRUE ");

        criteria.setIdLike(searchString(criteria.getIdLike()));
        criteria.setNumberLike(searchString(criteria.getNumberLike()));
        criteria.setAvatarUrlLike(searchString(criteria.getAvatarUrlLike()));
        criteria.setNickNameLike(searchString(criteria.getNickNameLike()));
        criteria.setRealNameLike(searchString(criteria.getRealNameLike()));
        criteria.setGenderLike(searchString(criteria.getGenderLike()));
        criteria.setPasswordLike(searchString(criteria.getPasswordLike()));
        criteria.setPhoneLike(searchString(criteria.getPhoneLike()));
        criteria.setStateLike(searchString(criteria.getStateLike()));

        if (criteria.getRecordId() != null) {
            buf.append("    AND RECORD_ID = " + criteria.getRecordId());
        }
        if (criteria.getUserUidEquals() != null) {
            buf.append("    AND USERUID = '" + criteria.getUserUidEquals() + "'");
        }
        if (criteria.getUserUidNotEquals() != null) {
            buf.append("    AND USERUID != '" + criteria.getUserUidNotEquals() + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getIdEquals())) {
            buf.append("    AND ID = '" + cut(criteria.getIdEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getIdNotEquals())) {
            buf.append("    AND ID != '" + cut(criteria.getIdNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getIdLike())) {
            buf.append("    AND ID LIKE '" + cut(criteria.getIdLike()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getNumberEquals())) {
            buf.append("    AND NUMBER = '" + cut(criteria.getNumberEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getNumberNotEquals())) {
            buf.append("    AND NUMBER != '" + cut(criteria.getNumberNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getNumberLike())) {
            buf.append("    AND NUMBER LIKE '" + cut(criteria.getNumberLike()) + "'");
        }
        if (criteria.getCreateTimeUpper() != null) {
            buf.append("    AND CREATETIME <= " + criteria.getCreateTimeUpper());
        }
        if (criteria.getCreateTimeLower() != null) {
            buf.append("    AND CREATETIME >= " + criteria.getCreateTimeLower());
        }
        if (criteria.getCreateTimeEquals() != null) {
            buf.append("    AND CREATETIME = '" + criteria.getCreateTimeEquals() + "'");
        }
        if (criteria.getCreateTimeNotEquals() != null) {
            buf.append("    AND CREATETIME != '" + criteria.getCreateTimeNotEquals() + "'");
        }
        if (criteria.getModifyTimeUpper() != null) {
            buf.append("    AND MODIFYTIME <= " + criteria.getModifyTimeUpper());
        }
        if (criteria.getModifyTimeLower() != null) {
            buf.append("    AND MODIFYTIME >= " + criteria.getModifyTimeLower());
        }
        if (criteria.getModifyTimeEquals() != null) {
            buf.append("    AND MODIFYTIME = '" + criteria.getModifyTimeEquals() + "'");
        }
        if (criteria.getModifyTimeNotEquals() != null) {
            buf.append("    AND MODIFYTIME != '" + criteria.getModifyTimeNotEquals() + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getAvatarUrlEquals())) {
            buf.append("    AND AVATARURL = '" + cut(criteria.getAvatarUrlEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getAvatarUrlNotEquals())) {
            buf.append("    AND AVATARURL != '" + cut(criteria.getAvatarUrlNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getAvatarUrlLike())) {
            buf.append("    AND AVATARURL LIKE '" + cut(criteria.getAvatarUrlLike()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getNickNameEquals())) {
            buf.append("    AND NICKNAME = '" + cut(criteria.getNickNameEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getNickNameNotEquals())) {
            buf.append("    AND NICKNAME != '" + cut(criteria.getNickNameNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getNickNameLike())) {
            buf.append("    AND NICKNAME LIKE '" + cut(criteria.getNickNameLike()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getRealNameEquals())) {
            buf.append("    AND REALNAME = '" + cut(criteria.getRealNameEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getRealNameNotEquals())) {
            buf.append("    AND REALNAME != '" + cut(criteria.getRealNameNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getRealNameLike())) {
            buf.append("    AND REALNAME LIKE '" + cut(criteria.getRealNameLike()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getGenderEquals())) {
            buf.append("    AND GENDER = '" + cut(criteria.getGenderEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getGenderNotEquals())) {
            buf.append("    AND GENDER != '" + cut(criteria.getGenderNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getGenderLike())) {
            buf.append("    AND GENDER LIKE '" + cut(criteria.getGenderLike()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getPasswordEquals())) {
            buf.append("    AND PASSWORD = '" + cut(criteria.getPasswordEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getPasswordNotEquals())) {
            buf.append("    AND PASSWORD != '" + cut(criteria.getPasswordNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getPasswordLike())) {
            buf.append("    AND PASSWORD LIKE '" + cut(criteria.getPasswordLike()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getPhoneEquals())) {
            buf.append("    AND PHONE = '" + cut(criteria.getPhoneEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getPhoneNotEquals())) {
            buf.append("    AND PHONE != '" + cut(criteria.getPhoneNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getPhoneLike())) {
            buf.append("    AND PHONE LIKE '" + cut(criteria.getPhoneLike()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getStateEquals())) {
            buf.append("    AND STATE = '" + cut(criteria.getStateEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getStateNotEquals())) {
            buf.append("    AND STATE != '" + cut(criteria.getStateNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getStateLike())) {
            buf.append("    AND STATE LIKE '" + cut(criteria.getStateLike()) + "'");
        }
        if (sort != null && sort.getColumns().size() > 0) {
            buf.append(" ORDER BY ");
            sort.getColumns().forEach(c -> buf.append(c.getColumnName().toUpperCase())
                    .append(" ").append((c.getAscending() != null && c.getAscending()) ? "ASC" : "DESC")
                    .append(", "));
            buf.append("RECORD_ID ASC");
        }
        return buf.toString();
    }

    @Override
    public <T extends Unit> String getUpdateStatement(T source) {
        GenShangUserListBean data = null;

        if (source instanceof com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListFromuserUidIF) {
            data = new GenShangUserListBean();
            ((com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListFromuserUidIF) source).getUserUidDataForGenShangUserList(data);
        }

        return data == null ? null : getLocalUpdateStatement(data);
    }
}
